<?php


namespace app\controller;


use think\facade\Request;

class member
{
    private $model;                 // 服务
    public function __construct()
    {
        $this->model = new \app\model\member();
    }
    public function getList()
    {
        $param=Request::get();
        $map = [];
        if (isset($param['name'])&& $name = trim($param['name'])) {
            $map[] = ["name", 'like', "%" . $name . "%"];
        }
        if (isset($param['type_id'])&&$param['type_id']){
            $map[] = ['type_id', '=', $param['type_id']];
        }
        $data=$this->model->with(['work'])->where($map)->paginate([
            'query' => $param,
            'var_page' => 'current_page',
            'list_rows' => $param['pagesize'],
        ])->toArray();
        if ($data) $meta=['status'=>200];
        else $meta=['status'=>400];
        return json(['data'=>$data,'meta'=>$meta]);
}

    public function delete($id)
    {
        // 删除
        $this->model->deleteData($id);
        return json(['meta'=>['msg'=>'删除成功','status'=>200]]);
}

    public function add()
    {
        $data = Request::post();
        $this->model->addData($data);
        return json(['meta'=>['msg'=>'新增成功','status'=>200]]);
}

    public function edit($id)
    {
        if (Request::isPut()) {
            // 获取POST数据
            $data = Request::put();
            // 入库

            $this->model->saveData($data);
            return json(['meta'=>['msg'=>'编辑成功','status'=>200]]);
        } else {
            // 如果没有get到id, 则跳转回列表页
//            $id = Request::get('id');
            // 当前设备信息
            $data = $this->model->find($id)->toArray();
            return json(['data'=>$data,'meta'=>['msg'=>'编辑成功','status'=>200]]);
}}
}